/**
 * Copyright 2009 Henning Teek, Ingo Renner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.bricket.plugin.mail.web;

import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.bricket.plugin.mail.domain.Mailserver;
import com.googlecode.bricket.plugin.mail.repository.MailService;
import com.googlecode.bricket.plugin.mail.web.editor.MailserverEditor;

public class MailPanel extends Panel {
	@SpringBean
	MailService mailService;

	private Component editor;

	private Mailserver currentMailserver = new Mailserver();

	public MailPanel(String id) {
		super(id);

		IModel<List<Mailserver>> listViewModel = new LoadableDetachableModel<List<Mailserver>>() {
			@Override
			protected List<Mailserver> load() {
				return mailService.loadAllMailserver();
			}
		};

		add(new MailserverListView("listView", listViewModel));
		setupEditor();

		add(new Link<Object>("newMailserver") {
			@Override
			public void onClick() {
				currentMailserver = new Mailserver();
				setupEditor();
			}

			@Override
			public boolean isEnabled() {
				return true;
			}
		});
	}

	private void setupEditor() {
		Component editor = new EditorPanel("editor", new PropertyModel<Mailserver>(this, "currentMailserver"));
		if (this.editor == null) {
			add(editor);
		} else {
			this.editor.replaceWith(editor);
		}
		this.editor = editor;
	}

	private class MailserverListView extends ListView<Mailserver> {
		public MailserverListView(String id, IModel<List<Mailserver>> model) {
			super(id, model);
		}

		@Override
		protected void populateItem(final ListItem<Mailserver> item) {
			Link<Object> select = new Link<Object>("select") {
				@Override
				public void onClick() {
					currentMailserver = item.getModelObject();
					setupEditor();
				}
			};
			select.add(new Label("label", item.getModelObject().getName()));
			item.add(select);
		}
	};

	private class EditorPanel extends Panel {
		public EditorPanel(String id, final IModel<Mailserver> model) {
			super(id, model);

			Form<Mailserver> form = new Form<Mailserver>("form", new CompoundPropertyModel<Mailserver>(model));
			add(form);

			form.add(new TextField<String>("name").setRequired(true));

			form.add(new MailserverEditor("editor", model));

			form.add(new SubmitLink("save") {
				@Override
				public void onSubmit() {
					mailService.saveMailserver(model.getObject());
					getSession().info(MailPanel.this.getString("mailserverSaved"));
				}
			});

			form.add(new SubmitLink("delete") {
				@Override
				public void onSubmit() {
					mailService.deleteMailserver(model.getObject());
					setupEditor();
				}

				@Override
				public boolean isVisible() {
					return true;
				}
			}.setDefaultFormProcessing(false));

			add(new FeedbackPanel("feedback"));
		}
	};
}
